"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { deleteSession, DeleteSessionRes } from "@/lib/api/session";

export function useDeleteSession() {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const router = useRouter();

  const deleteSessionById = async (sessionId: string): Promise<DeleteSessionRes> => {
    setError(null);
    setLoading(true);
    
    try {
      const result = await deleteSession(sessionId);
      
      // Navigate to dashboard after successful deletion
      router.push('/dashboard');
      
      return result;
    } catch (err: any) {
      const errorMessage = err?.message || "Failed to delete session";
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  };

  return {
    deleteSession: deleteSessionById,
    loading,
    error,
  };
}
